package com.buss.user.service.impl;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.AddressUtils;
import org.jeecgframework.core.util.BrowserUtils;
import org.jeecgframework.core.util.ContextHolderUtils;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.EncryptUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.buss.user.entity.BnsUsersInfoEntity;
import com.buss.user.entity.UserDynamicSetEntity;
import com.buss.user.entity.UserLoginLog;
import com.buss.user.entity.UserPrivacySet;
import com.buss.user.service.BnsUsersInfoServiceI;

@Service("bnsUsersInfoService")
@Transactional
public class BnsUsersInfoServiceImpl extends CommonServiceImpl implements BnsUsersInfoServiceI {

	@Autowired
	private CommonService commonService;
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((BnsUsersInfoEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((BnsUsersInfoEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((BnsUsersInfoEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(BnsUsersInfoEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(BnsUsersInfoEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(BnsUsersInfoEntity t){
	 	return true;
 	}
 	

 	/**
 	 * 查询当前用户是否存在，验证 密码和安全问题
 	 */
 	public BnsUsersInfoEntity checkUserExits(String username, String password, String ask, String ans){
 		List<BnsUsersInfoEntity> resultListR = null;
		if(!StringUtil.isEmpty(username) && !StringUtil.isEmpty(password)){
			password = EncryptUtil.encrypt(password, "MD5");
			String sqlStr = "from BnsUsersInfoEntity where userName = ? and passWord = ?  ";	// 限制 10条   LIMIT 10
			if(!ask.equals("0")){
				sqlStr += " and ask = ? and ans =?";
				resultListR = commonService.findHql(sqlStr, username,password,ask,ans);
			}else{
				resultListR = commonService.findHql(sqlStr, username,password);
			}
		}
		if(resultListR.size() > 0){
			return resultListR.get(0);
		}
		return null;
 	}
 	
 	/**
 	 * 根据ID查找用户
 	 * @param id
 	 * @return
 	 */
 	public BnsUsersInfoEntity findSingleResult(String phone){
 		return commonService.findUniqueByProperty(BnsUsersInfoEntity.class, "mobile", phone);
 	}
 	
 	/**
	 * 添加用户登录日志
	 */
	public void addUserLog(String userId){
		HttpServletRequest request = ContextHolderUtils.getRequest();
		String broswer = BrowserUtils.checkBrowse(request);
		UserLoginLog userLog = new UserLoginLog();
//		userLog.setId(UUID.randomUUID().toString());
		userLog.setUserId(userId); 
		Date time = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		String loginTime = formatter.format(time);
		userLog.setLoginTime(loginTime);
		String ip = oConvertUtils.getIp();
		userLog.setLoginIp(ip);
		String loginArea = null;
		try {
			loginArea = AddressUtils.getAddresses("ip="+ip, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if(!StringUtil.isEmpty(loginArea)){
			userLog.setLoginArea(loginArea);
		}
		commonDao.save(userLog);
	}

	/**
	 * 初始化用户设置
	 */
	@Override
	public void userSetInit(Integer userId) {
		UserPrivacySet userPrivacySet = new UserPrivacySet();
		userPrivacySet.setUserId(userId); 
		userPrivacySet.setFriendList(0);
		userPrivacySet.setMessageBoard(0);
		userPrivacySet.setDynamicState(0);
		userPrivacySet.setRecord(0);
		userPrivacySet.setLog(0);
		userPrivacySet.setPhotoAlbum(0);
		userPrivacySet.setShare(0);
		userPrivacySet.setSetTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		commonService.save(userPrivacySet);
	}
	
	/**
	 * 初始化动态设置
	 */
	@Override
	public void userDynamicInit(Integer userId) {
		UserDynamicSetEntity userDynamicSet = new UserDynamicSetEntity();
		userDynamicSet.setUserId(userId); 
		userDynamicSet.setRecord(1);
		userDynamicSet.setLog(1);
		
		userDynamicSet.setUploadImg(1);
		userDynamicSet.setAddShare(1);
		userDynamicSet.setAddFirend(1);
		
		userDynamicSet.setComment(1);
		userDynamicSet.setPaidListing(1);
		userDynamicSet.setPointConsumption(1);
		
		userDynamicSet.setInviteFriends(1);
		userDynamicSet.setCompletionTask(1);
		userDynamicSet.setUpdateInfor(1);
		
		userDynamicSet.setRzDeclare(1);
		userDynamicSet.setForumFt(0);
		userDynamicSet.setForumHt(0);
		userDynamicSet.setSetTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		commonService.save(userDynamicSet);
	}
	
	
	
	
 	
}